class Solution:
    def maxProduct(self, words: list) -> int:
        res, length = 0, len(words)
        for i in range(length):
            words[i] = [set(words[i]), len(words[i])]
        for i in range(length):
            for j in range(i+1, length):
                for alpha in words[j][0]:
                    if alpha in words[i][0]:
                        break
                else:
                    res = max(res, words[j][1] * words[i][1])
        return res


class Solution:
    """写法有问题,并没有快多少"""
    alpha_dict = {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11,
                  "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17, "s": 18, "t": 19, "u": 20, "v": 21,
                  "w": 22, "x": 23, "y": 24, "z": 25}

    def maxProduct(self, words: list) -> int:
        res, length = 0, len(words)
        for i in range(length):
            mid_set = set(words[i])
            leng, num = len(words[i]), 0
            for alpha in mid_set:
                num += 2 ** Solution.alpha_list[alpha]
            words[i] = [num, leng]
        for i in range(length):
            for j in range(i+1, length):
                if words[i][0] & words[j][0] == 0:
                    res = max(res, words[j][1] * words[i][1])
        return res


class Solution:
    alpha_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
                  "u", "v", "w", "x", "y", "z"]

    def maxProduct(self, words: list) -> int:
        res, length = 0, len(words)
        for i in range(length):
            word = words[i]
            word_set = set(word)
            leng, num = len(word), 0
            for alpha in Solution.alpha_list:
                num = num << 1
                if alpha in word_set:
                    num += 1
            words[i] = [num, leng]
        for i in range(length):
            for j in range(i+1, length):
                if words[i][0] & words[j][0] == 0:
                    res = max(res, words[j][1] * words[i][1])
        return res


a = Solution()
print(a.maxProduct(["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]))
print(a.maxProduct(["a","ab","abc","d","cd","bcd","abcd"]))
print(a.maxProduct(["a","aa","aaa","aaaa"]))
